package view;

import javax.swing.JPanel;

import control.OrderControl;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JSlider;
import javax.swing.JScrollPane;
import java.util.LinkedList;

import javax.swing.JComboBox;
import javax.swing.JButton;

import java.awt.Color;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class SearchPalletTime extends JPanel {
	private OrderControl oc;
	private JComboBox comboBox;
	private JSlider slider_1;
	private JSlider slider;
	private JScrollPane scrollPane;
	private JTextArea textArea;
	private JLabel lblTime;
	private JSlider slider_2;
	private JSlider slider_3;

	public SearchPalletTime(OrderControl oc) {
		setBackground(Color.BLACK);
		this.oc = oc;
		
		JLabel lblDate = new JLabel("Date");
		lblDate.setForeground(Color.GREEN);
		
		JLabel lblStart = new JLabel("Start");
		lblStart.setForeground(Color.GREEN);
		
		slider = new JSlider(1,365);
		slider.setToolTipText("");
		slider.setBackground(Color.BLACK);
		slider.setForeground(Color.GREEN);
		slider.setMajorTickSpacing(73);
		slider.setMinorTickSpacing(20);
	    slider.setPaintLabels(true);
		
		JLabel lblEnd = new JLabel("End");
		lblEnd.setForeground(Color.GREEN);
		
		slider_1 = new JSlider(1, 365);
		slider_1.setBackground(Color.BLACK);
		slider_1.setForeground(Color.GREEN);
		slider_1.setMajorTickSpacing(73);
		slider_1.setMinorTickSpacing(20);
	    slider_1.setPaintLabels(true);
	    
	    textArea = new JTextArea();
	    textArea.setForeground(Color.GREEN);
	    textArea.setBackground(Color.BLACK);
	    textArea.setEditable(false);
		scrollPane = new JScrollPane(textArea);
		
		
		String[] years = {"2010","2011"};
		comboBox = new JComboBox(years);
		
		
		JLabel lblYear = new JLabel("Year");
		lblYear.setForeground(Color.GREEN);
		
		JButton btnGetPallets = new JButton("Get pallets");
		btnGetPallets.addActionListener(new GetPallets());
		
		lblTime = new JLabel("Time");
		lblTime.setForeground(Color.GREEN);
		
		slider_2 = new JSlider(1,24);
		slider_2.setBackground(Color.BLACK);
		slider_2.setForeground(Color.GREEN);
		slider_2.setMajorTickSpacing(6);
		slider_2.setMinorTickSpacing(4);
	    slider_2.setPaintLabels(true);
		
		slider_3 = new JSlider(1,24);
		slider_3.setBackground(Color.BLACK);
		slider_3.setForeground(Color.GREEN);
		slider_3.setMajorTickSpacing(6);
		slider_3.setMinorTickSpacing(4);
	    slider_3.setPaintLabels(true);
		
		GroupLayout groupLayout = new GroupLayout(this);
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(38)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 706, Short.MAX_VALUE)
						.addGroup(groupLayout.createSequentialGroup()
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblYear)
							.addGap(18)
							.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
							.addGap(18)
							.addComponent(btnGetPallets))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblTime)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(slider_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(slider_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addGroup(groupLayout.createSequentialGroup()
							.addComponent(lblDate)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(slider, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblStart))
							.addPreferredGap(ComponentPlacement.RELATED)
							.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
								.addComponent(lblEnd)
								.addComponent(slider_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblStart)
						.addComponent(lblEnd))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblDate)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(slider, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(slider_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(36)
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addComponent(lblTime)
						.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
							.addComponent(slider_2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addComponent(slider_3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
					.addGap(31)
					.addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
						.addComponent(lblYear)
						.addComponent(comboBox, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(btnGetPallets))
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 226, Short.MAX_VALUE)
					.addContainerGap())
		);
		setLayout(groupLayout);
		// TODO Auto-generated constructor stub
	}
	
	private class GetPallets implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			int from_date = slider.getValue();
			int to_date = slider_1.getValue();
			int from_time = slider_2.getValue();
			int to_time = slider_3.getValue();
			String year = (String) comboBox.getSelectedItem();
			LinkedList<String> infoList = oc.getPalletDate(from_date,to_date,year, from_time, to_time);
			textArea.setText("");
			for(String info:infoList){
				textArea.append(info + "\n");
			}
			
		}
		
	}
}
